	package com.cn.hnust.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cn.hnust.pojo.Column;
import com.cn.hnust.pojo.Message;
import com.cn.hnust.pojo.Scenic;
import com.cn.hnust.pojo.Strategy;
import com.cn.hnust.pojo.Tavern;
import com.cn.hnust.pojo.User;
import com.cn.hnust.service.IColumnService;
import com.cn.hnust.service.IMessageService;
import com.cn.hnust.service.IScenicService;
import com.cn.hnust.service.IStrategyService;
import com.cn.hnust.service.ITavernService;
import com.cn.hnust.service.IUserService;

@Controller
@RequestMapping("/strategy")
public class StrategyController {
	@Autowired
	private IStrategyService strategyService;
	@Autowired
	private IScenicService scenicService;

	@Autowired
	private IMessageService messageservice;

	@Autowired
	private IUserService userService;

	@Autowired
	private IColumnService ColumnService;
	
	@Autowired
	private ITavernService TavernService;
	
	@RequestMapping(value = "/xqStrategy")
	public String xqstrategy(Model model, int strategy_id,HttpServletRequest request) {
		Strategy strategy = strategyService.getUser(strategy_id);
			model.addAttribute("strategy", strategy);
			
	    return "/reception/mddxym";  
			}

//	@RequestMapping(value = "/StrategyList")
//	public String toIndex(Model model) throws Exception {
//		List<Strategy> strategy = strategyService.getUsers();
//		model.addAttribute("strategy", strategy);
//
//		return "reception/mdd";
//	}

	@RequestMapping(value = "/qtindex")
	public String jqrecord(Model model) {

		List<Scenic> scenic = scenicService.queryReception();// 取得当前用户的所有操作记录

		model.addAttribute("scenic", scenic);

		return "/reception/index";
	}

	@RequestMapping(value = "/jqpurshase")
	public String jqpurshase(Model model, int scenic_id) {

		Scenic scenic = scenicService.getUser(scenic_id);
		Message message = messageservice.getUser("太阳山");

		int message_num = messageservice.getCounts(scenic.getScenic_name());
		model.addAttribute("scenic", scenic);
		model.addAttribute("message", message);
		model.addAttribute("message_num", message_num);
		return "/reception/purchase";
	}

	@RequestMapping(value = "/jqzf")
	public String jqzf() {

		return "/reception/demo";
	}

	@RequestMapping(value = "/gaode")
	public String gaode() {

		return "/reception/degao";
	}

	@RequestMapping(value = "/person")
	public String person() {

		return "/reception/person";
	}

	@RequestMapping(value = "/card")
	public String card() {

		return "/reception/card";
	}

	@RequestMapping(value = "/subarea")
	public String subarea(Model model, String scenic_dq) {
		List<Scenic> scenic = scenicService.getScenic_dq(scenic_dq);
		model.addAttribute("scenic", scenic);

		return "/reception/subarea";
	}

	@RequestMapping(value = "/bourn")
	public String bourn(Model model) {
		List<Strategy> strategy = strategyService.getUsers();
		model.addAttribute("strategy", strategy);
		strategy.iterator();
		
		return "/reception/bourn";
	}

	@RequestMapping(value = "/login")
	public String login(Model model) {

		return "/reception/login";
	}

	@RequestMapping(value = "/logins")
	@ResponseBody
	public Map<String, Object> logins(String userName, String password, HttpServletRequest request) {

		Map<String, Object> Map = new HashMap<String, Object>();

		User u = userService.getUser(userName);
		HttpSession session = request.getSession();
		session.setAttribute("userid", u.getUserName());
		if (userName.equals(u.getUserName()) && password.equals(u.getPassword())) {

			Map.put("userName", userName);
			Map.put("password", password);
		} else {
			Map.put("shibai", "失败");
		}
		return Map;
	}

	@RequestMapping(value = "/forgetpw")
	public String forgetpw() {

		return "/reception/getpassw";
	}
	
	@RequestMapping(value = "/getpw")
	 @ResponseBody//此处不能省略 否则ajax无法解析返回值  
	public  Map<String,Object> getpw(String userName) {
		Properties properties = new Properties();
		properties.setProperty("mail.transport.protocol", "smtp");// 发送邮件协议
		properties.setProperty("mail.smtp.auth", "true");// 需要验证
		properties.setProperty("mail.debug", "true");// 设置debug模式 后台输出邮件发送的过程
		Session session = Session.getInstance(properties);
		session.setDebug(true);// debug模式
		// 邮件信息
		MimeMessage messgae = new MimeMessage(session);
		User u = userService.getUser(userName);
		try {
			messgae.setFrom(new InternetAddress("194113961@qq.com"));
		} catch (AddressException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // 设置发送人
		try {
			messgae.setText("用户："+u.getUserName()+"密码："+u.getPassword());
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // 设置邮件内容
		try {
			messgae.setSubject("微常德休闲秘书");
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // 设置邮件主题
			// 发送邮件
		Transport tran = null;
		try {
			tran = session.getTransport();
		} catch (NoSuchProviderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// tran.connect("smtp.sina.com", 25, "邮箱账户", "邮箱密码");//连接到新浪邮箱服务器
		try {
			tran.connect("smtp.qq.com", 587, "194113961@qq.com", "pujfzzjeevrfbiga");
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // 连接到QQ邮箱服务器
		try {
			tran.sendMessage(messgae, new Address[] { new InternetAddress(userName) });
		} catch (AddressException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // 设置邮件接收人
		try {
			tran.close();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Map<String,Object> resultMap = new HashMap<String, Object>(); 
		 resultMap.put("userName", u.getUserName());  
		return resultMap;
	}
	
	@RequestMapping(value = "/columnq")
	public String columnq(Model model) {
		List<Column> column = ColumnService.getUsers();
		model.addAttribute("column", column);
		return "/reception/columnq";
	}
	
	@RequestMapping(value = "/tavernq")
	public String tavernq(Model model) {
		List<Tavern> tavern = TavernService.getUsers();
		model.addAttribute("tavern", tavern);
		return "/reception/tavernq";
	}
	
	@RequestMapping(value = "/tavernxq")
	public String tavernxq(Model model,int tavern_id) {
		Tavern tavern = TavernService.getUser(tavern_id);
		model.addAttribute("tavern", tavern);
		return "/reception/tavernxq";
	}

	@RequestMapping(value = "/columnxq")
	public String columnxq(Model model,int column_id) {
		Column column = ColumnService.getUser(column_id);
		model.addAttribute("column", column);
		return "/reception/columnxq";
	}
	
	@RequestMapping(value = "/register")
	public String register() {
		
		
		return "/reception/register";
	}
	
	@RequestMapping(value = "/registers")
	@ResponseBody
	public Map<String,Object> registers(String userName,String password) {
		Map<String,Object> resultMap = new HashMap<>();
		User user = new User();
		user.setUserName(userName);
		user.setPassword(password);
		int a = userService.insert(user);
		if(a != 0){
			resultMap.put("success", "注册成功");
		}else{
			resultMap.put("error", "注册失败");	
		}
		return resultMap;
	}
	
	@RequestMapping(value = "/persons")
	@ResponseBody
	public Map<String, Object> persons(HttpSession session) {  
		Map<String,Object> resultMap = new HashMap<>();
		resultMap.put("msg", "1");    
		session.removeAttribute("userid");
		
		return resultMap;
	}
}
